package com.yoona.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoona.common.enums.CommonEnums;
import com.yoona.common.response.BaseResponse;
import com.yoona.common.utils.HttpServletRequestUtil;
import com.yoona.system.mapper.DynamicCommentsMapper;
import com.yoona.system.mapper.DynamicMapper;
import com.yoona.system.dto.WriteCommentsDto;
import com.yoona.system.entity.DynamicComments;
import com.yoona.system.service.IDynamicCommentsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Email: m15602498163@163.com
 * @Author: yoonada
 * @Date: 2020/9/21
 * @Time: 8:48 下午
 * @Msg:
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DynamicCommentsServiceImpl extends ServiceImpl<DynamicCommentsMapper, DynamicComments> implements IDynamicCommentsService {


    private final DynamicMapper dynamicMapper;

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/18 4:58 下午
     * @Param: [writeCommentsDto]
     * @Return: com.yoona.common.response.SystemResponse
     * @Description: 写评论
     */
    @Override
    public BaseResponse<Object> writeComments(WriteCommentsDto writeCommentsDto) {
        HttpServletRequest request = HttpServletRequestUtil.getHttpServletRequest();
        String uid = (String) request.getAttribute("uid");
        String nickName = (String) request.getAttribute("nick_name");
        DynamicComments dynamicComments = DynamicComments
                .builder()
                .commenterId(uid)
                .commenterNickName(nickName)
                .commentCreateTime(new Date())
                .build();
        BeanUtils.copyProperties(writeCommentsDto,dynamicComments);
        int num = this.baseMapper.insert(dynamicComments);
        if (num != 0) {
            int i = dynamicMapper.updateCommentTotalNum(writeCommentsDto.getDynamicId());
            if (i == 1){
                return BaseResponse.success(CommonEnums.INSERT_SUCCESS);
            }else {
                return BaseResponse.error(CommonEnums.INSERT_ERROR);
            }
        } else {
            return BaseResponse.error(CommonEnums.INSERT_ERROR);
        }
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/18 4:58 下午
     * @Param: [commentId]
     * @Return: com.yoona.common.response.SystemResponse
     * @Description: 判断是否能删除这条评论/评论的回复
     */
    @Override
    public BaseResponse<Object> whetherCanDeleteThisComments(String commentId) {
        HttpServletRequest request = HttpServletRequestUtil.getHttpServletRequest();
        String uid = (String) request.getAttribute("uid");
        QueryWrapper<DynamicComments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id",commentId);
        DynamicComments comments = this.baseMapper.selectOne(queryWrapper);
        if (comments.getCommenterId().equals(uid)){
            comments.setCommentCanDelete(1);
        }
        return BaseResponse.success(comments);
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/18 4:56 下午
     * @Param: [commentId, dynamicId]
     * @Return: com.yoona.common.response.SystemResponse
     * @Description: 根据评论id删除评论
     */

    @Override
    public BaseResponse<Object> deleteComments(String commentId,String dynamicId) {
        Map<String,Object> map=new HashMap<>(1);
        map.put("comment_id",commentId);
        int i = this.baseMapper.deleteByMap(map);
        if (i==1){
            int num = dynamicMapper.updateCommentDeleteTotalNum(dynamicId);
            if (num ==1){
                return BaseResponse.success(CommonEnums.DELETE_SUCCESS);
            }else {
                return BaseResponse.error(CommonEnums.DELETE_ERROR);
            }
        }else {
            return BaseResponse.error(CommonEnums.DELETE_ERROR);
        }
    }
}
